package com.example.prison.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.LoginInterceptor;
import com.example.prison.config.Response;
import com.example.prison.entity.UseCarManagement;
import com.example.prison.service.SystemLogService;
import com.example.prison.service.UseCarManagementService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@Slf4j
@Api(tags = "用车管理")
@RestController
@RequestMapping("/api")
public class UseCarManagementController {

    @Autowired
    private UseCarManagementService useCarManagementService;

    @Autowired
    private SystemLogService systemLogService;

    @ApiOperation("获取使用车辆列表")
    @GetMapping("/getUseCar")
    public Response<PageInfo<UseCarManagement>> getUseCar(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                          @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                          @Parameter(description = "车牌号")@RequestParam(required = false) String carNumber,
                                                          @Parameter(description = "使用人")@RequestParam(required = false) String usePeople,
                                                          @Parameter(description = "状态（未审批/同意/不同意）")@RequestParam(required = false) String status,
                                                          @Parameter(description = "用车范围(市区用车/长途用车）")@RequestParam(required = false) String useRange,
                                                          @Parameter(description = "使用状态（未出发/用车中/已返回）")@RequestParam(required = false) String useStatus,
                                                          @Parameter(description = "开始时间")@RequestParam(defaultValue = "0000-00-00") String beginTime,
                                                          @Parameter(description = "结束时间")@RequestParam(defaultValue = "9999-12-31") String endTime) {
        PageInfo<UseCarManagement> pageInfo = useCarManagementService.get(pageNumber, pageSize, carNumber, usePeople, status, useRange, useStatus, beginTime, endTime);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("用车申请")
    @PostMapping("/addUseCar")
    public Response<?> addUseCar(@RequestBody UseCarManagement useCar) {
        if(!Objects.equals(LoginInterceptor.loginUser.getRoleName(), "超级管理员") && !Objects.equals(LoginInterceptor.loginUser.getDepartment(), useCar.getDepart())) {
            return Response.error("无此权限！");
        }
        if (useCar.getUseTime().compareTo(useCar.getEndTime()) > 0) {
            return Response.error("时间输入错误！请检查时间！");
        }
        int result = useCarManagementService.add(useCar);
        if (result == -1) {
            return Response.error("该车辆该时间已被申请！");
        }
        log.info("用车申请"+", 数据："+ JSON.toJSONString(useCar));
        systemLogService.add("用车申请");
        return Response.success("申请提交成功！");
    }

    @ApiOperation("依据id删除使用车辆数据")
    @PostMapping("/deleteUseCar")
    public Response<?> deleteUseCar(@Parameter(description = "ID") @RequestBody Integer[] ids) {
        useCarManagementService.delete(ids);
        log.info("删除使用车辆数据");
        systemLogService.add("删除使用车辆数据");
        return Response.success("删除成功！");

    }

    @ApiOperation("编辑使用车辆")
    @PostMapping("/updateUseCar")
    public Response<?> updateUseCar(@RequestBody UseCarManagement useCar) {
        if(useCar.getUseTime().compareTo(useCar.getEndTime()) > 0) {
            return Response.error("时间顺序错误！请重新输入");
        }
        Integer id = useCar.getId();
        int result = useCarManagementService.update(useCar, id);
        if (result == 0) {
            return Response.error("该车辆该时间已被申请！");
        }
        log.info("编辑使用车辆"+", 数据："+JSON.toJSONString(useCar));
        systemLogService.add("编辑使用车辆");
        return Response.success("更新成功！");
    }

    @ApiOperation("依据id获取用车信息")
    @GetMapping("/getUseCarById")
    public Response<UseCarManagement> getUseCarById(@Parameter(description = "ID")@RequestParam Integer id) {
        UseCarManagement useCar = useCarManagementService.getById(id);
        if(ObjectUtils.isEmpty(useCar)) {
            return Response.error("此使用车辆id不存在！");
        }
        return Response.success("获取成功！", useCar);
    }

    @ApiOperation("获取到期未归用车")
    @GetMapping("/getNoReturnUseCar")
    public Response<PageInfo<UseCarManagement>> getNoReturnUseCar(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                  @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        PageInfo<UseCarManagement> pageInfo = useCarManagementService.getNoReturn(pageNumber, pageSize);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("获取使用中长途用车")
    @GetMapping("/getUsingLongCar")
    public Response<PageInfo<UseCarManagement>> getUsingLongCar(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        PageInfo<UseCarManagement> pageInfo = useCarManagementService.getUsingLongCar(pageNumber, pageSize);
        return Response.success("获取成功！", pageInfo);
    }

}

